<template>
  <view class="container">
    <!-- 搜索框 -->
    <view class="search-box">
      <input type="text" placeholder="搜索药品" v-model="searchValue" @input="handleSearch" />
    </view>
    <!-- 分类栏 -->
    <view class="category-bar">
      <text class="category-item" :class="{ active: currentCategory === '全部' }" @click="currentCategory = '全部'">全部</text>
      <picker :range="categoryList" :value="categoryIndex" @change="handleCategoryChange">
        <text class="category-item dropdown">下拉框</text>
      </picker>
    </view>
    <!-- 商品列表 -->
    <view class="goods-list">
      <view class="goods-item" v-for="(item, index) in filteredGoods" :key="index">
        <image :src="item.medicineCover" class="goods-image" mode="aspectFill" />
        <view class="goods-info">
          <text class="goods-name">{{ item.medicineName }}</text>
          <text class="goods-price">¥{{ item.medicinePrice }}</text>
          <text class="goods-category">分类：{{ item.medicineCategoryName }}</text>
          <text class="goods-desc">{{ item.medicineDescription }}</text>
          <text class="goods-usage">用法：{{ item.medicineUsage }}</text>
          <text class="goods-dosage">用量：{{ item.medicineDosage }}</text>
        </view>
      </view>
    </view>
    <!-- 底部栏 -->
    <view class="bottom-bar">
      <text class="amount">金额：¥{{ totalAmount.toFixed(2) }}</text>
      <button class="settle-btn" @click="handleSettle">结算</button>
    </view>
  </view>
</template>

<script>
import { list } from '@/api/shop.js'; // 假设 api 文件路径为 @/api/api.js

export default {
  data() {
    return {
      searchValue: '', // 搜索值
      currentCategory: '全部', // 当前分类
      categoryList: [], // 分类列表
      categoryIndex: 0, // 分类选择索引
      goodsList: [], // 所有商品列表
      cartList: [], // 购物车列表
    };
  },
  computed: {
    // 筛选后的商品列表
    filteredGoods() {
      let result = this.goodsList;
      // 搜索过滤
      if (this.searchValue) {
        result = result.filter(item =>
          item.medicineName.includes(this.searchValue) ||
          item.medicineCategoryName.includes(this.searchValue) ||
          item.medicineDescription.includes(this.searchValue)
        );
      }
      // 分类过滤
      if (this.currentCategory !== '全部') {
        result = result.filter(item => item.medicineCategoryName === this.currentCategory);
      }
      return result;
    },
    // 计算总金额
    totalAmount() {
      return this.cartList.reduce((total, item) => total + item.medicinePrice * item.count, 0);
    },
  },
  methods: {
    // 搜索处理
    handleSearch() {
      // 可以在这里做实时搜索，或者延迟搜索
    },
    // 分类选择变化
    handleCategoryChange(e) {
      const index = e.detail.value;
      this.categoryIndex = index;
      this.currentCategory = this.categoryList[index];
    },
    // 结算处理
    handleSettle() {
      if (this.cartList.length === 0) {
        uni.showToast({
          title: '购物车为空',
          icon: 'none',
        });
        return;
      }
      // 跳转到结算页面，这里可以传递购物车数据
      uni.navigateTo({
        url: '/pages/settle/settle?cartList=' + JSON.stringify(this.cartList),
      });
    },
    // 获取商品列表
    getGoodsList() {
      list({}).then(res => {
        if (res.code === 200) {
          this.goodsList = res.data;
          // 获取分类列表
          const categories = new Set(res.data.map(item => item.medicineCategoryName));
          this.categoryList = ['全部', ...categories];
        } else {
          uni.showToast({
            title: res.msg || '获取商品列表失败',
            icon: 'none',
          });
        }
      }).catch(err => {
        uni.showToast({
          title: '网络错误',
          icon: 'none',
        });
        console.error(err);
      });
    },
  },
  onLoad() {
    this.getGoodsList();
  },
};
</script>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.search-box {
  padding: 10px;
  background-color: #f5f5f5;
}
.search-box input {
  width: 100%;
  height: 40px;
  border-radius: 20px;
  border: 1px solid #ddd;
  padding: 0 15px;
}
.category-bar {
  display: flex;
  align-items: center;
  padding: 10px;
  background-color: #f5f5f5;
  border-bottom: 1px solid #ddd;
}
.category-item {
  margin-right: 15px;
  padding: 5px 10px;
  border-radius: 4px;
}
.category-item.active {
  background-color: #007aff;
  color: #fff;
}
.dropdown {
  border: 1px solid #ddd;
  padding: 5px 10px;
  border-radius: 4px;
}
.goods-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
}
.goods-item {
  display: flex;
  margin-bottom: 15px;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
}
.goods-image {
  width: 120px;
  height: 120px;
  margin-right: 10px;
  border-radius: 4px;
}
.goods-info {
  flex: 1;
}
.goods-name {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 5px;
  display: block;
}
.goods-price {
  color: #f00;
  font-size: 14px;
  margin-bottom: 5px;
  display: block;
}
.goods-category, .goods-desc, .goods-usage, .goods-dosage {
  font-size: 12px;
  color: #666;
  margin-bottom: 3px;
  display: block;
}
.bottom-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px;
  background-color: #f5f5f5;
  border-top: 1px solid #ddd;
}
.amount {
  font-size: 16px;
}
.settle-btn {
  background-color: #007aff;
  color: #fff;
  padding: 8px 20px;
  border-radius: 4px;
}
</style>